package cn.tedu.luban.order.webapi.service.impl;

import cn.tedu.luban.commons.pojo.order.dto.OrderMqDTO;
import cn.tedu.luban.order.service.EventService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class EventServiceImpl implements EventService {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Value("${mq.topic.request-order-cancel-topic:request-order-cancel-topic}")
    private String cancelTopic;

    @Value("${mq.topic.order-success-topic:order-success-topic}")
    private String orderSettleTopic;

    @Override
    public void sendOrderCancelEvent(OrderMqDTO orderMqDTO) {
        System.out.println("****************************"+cancelTopic);
        rocketMQTemplate.asyncSend(cancelTopic,
                orderMqDTO.getRequestOrderNo(), new SendCallback() {
                    @Override
                    public void onSuccess(SendResult result) {
                        log.info("发送mq成功, topic: {}, result:{}", cancelTopic, result);
                    }
                    @Override
                    public void onException(Throwable throwable) {
                        log.error("发送mq失败, topic: {}, Throwable", cancelTopic, throwable);
                    }
                });
    }
    @Override
    public void sendOrderFinishEvent(OrderMqDTO orderMqDTO) {
        rocketMQTemplate.asyncSend(orderSettleTopic, orderMqDTO, new SendCallback() {
            @Override
            public void onSuccess(SendResult result) {
                log.info("发送mq成功, topic: {}, result:{}", orderSettleTopic, result);
            }

            @Override
            public void onException(Throwable throwable) {
                log.error("发送mq失败, topic: {}, Throwable", orderSettleTopic, throwable);
            }
        });
    }


}
